//
//  LoginViewController.m
//  豆瓣
//
//  Created by dllo on 15/12/7.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "LoginViewController.h"
#import "LoginView.h"
#import "RegisterViewController.h"

@interface LoginViewController ()<LoginViewDelegate, RegisterViewControllerDelegate>

@property (nonatomic, retain) LoginView *loginV;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"用户登录";
    self.loginV = [[LoginView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:_loginV];
    
    
    UIImage *image = [[UIImage imageNamed:@"btn_nav_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonAction:)];
//    self.navigationItem.leftBarButtonItem.title = @"返回";
//    [self.navigationItem.leftBarButtonItem setTintColor:[UIColor blackColor]];
    
    self.loginV.myDelegate = self;
    
}
- (void)leftBarButtonAction:(UIBarButtonItem *)button{
    
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}

- (void)loginAlert{
    NSString *accout = [[NSUserDefaults standardUserDefaults] objectForKey:@"account"];
    NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];
    if ([accout isEqualToString:self.loginV.usernameText.text] && [password isEqualToString:self.loginV.passwordText.text]) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"登录成功" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *alertAc = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            
        }];
        [self.myDelegate changeRightButton];
        [alert addAction:alertAc];
        [self presentViewController:alert animated:YES completion:^{
            
            
        }];
        
        [[NSUserDefaults standardUserDefaults] setObject:@"yes" forKey:@"BOOL"];
        // 强制保存, 系统有一定的延迟保存
        [[NSUserDefaults standardUserDefaults] synchronize];
//        [self dismissViewControllerAnimated:YES completion:^{
//            
//            
//        }];
        [self dismissViewControllerAnimated:YES completion:^{
            
            
        }];
        
        
    } else {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"账户或密码错误" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *alertAc = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            
        }];
        [alert addAction:alertAc];
        [self presentViewController:alert animated:YES completion:^{
            
        }];
    }

    
}

- (void)pushToRegisterView{
    RegisterViewController *registerVC = [[RegisterViewController alloc] init];

    registerVC.myDelegate = self;
//    NSLog(@"++++++");
//    [self presentViewController:registerVC animated:YES completion:^{
//        
//        
//        
//    }];

    [self.navigationController pushViewController:registerVC animated:YES];
    
    
}
- (void)dismissToUserView{
    [self dismissViewControllerAnimated:YES completion:^{
        
        
        
    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
